import React, { Component } from 'react';
import '../css/School.less';
import {observer} from "mobx-react";
import  Mobx from  '../mobx'
import  HeaderImg  from  './HeaderImg'
import  Dibu from  './dibu'
import axios from 'axios'
@observer class School extends Component {
    constructor(props) {
        super(props)
        this.state={
            items:['光阴的故事','追梦的人，从这里开始'],
            size:0,
            total:1,
            School:[]
        }
    }
    componentDidMount(){
        Mobx.history= this.props.history
        axios.get('http://www.niuduxiu.com/authentication/findzuzhi.jhtml?cpageNumber=1&cpageSize=1&zpageNumber=1&zpageSize=6&type='+this.props.match.params.id)
            .then((item)=>{
                let total=Math.ceil(item.data.total/6)
                console.log(total)
                this.setState({
                    School:item.data.zuzhilist,
                    size:item.data.pageable.pageNumber,
                    total:total
                },()=>{
                    if(this.state.total>this.state.size){
                        this.interval()
                    }
                })
            })
    }
    interval(){
        if(this.state.total<=this.state.size){
            return false;
        }
        var interval1=setInterval(()=>{
            if((document.documentElement.scrollTop||document.body.scrollTop)-document.body.scrollHeight>=-1000){
                clearInterval(interval1)
                this.setState({
                    size:this.state.size+1
                },()=>{
                    this.loading()
                })
            }
        },100)
    }
    loading(){
        if(this.state.size===1){
            axios.get('http://www.niuduxiu.com/authentication/findzuzhi.jhtml?cpageNumber=1&cpageSize=1&zpageNumber='+this.state.size+'&zpageSize=6&type='+this.props.match.params.id)
                .then((item)=>{
                    let total=Math.ceil(item.data.total/6)
                    console.log(total)
                    this.setState({
                        School:item.data.zuzhilist,
                        size:item.data.pageable.pageNumber,
                        total:total
                    },()=>{
                        if(this.state.total>this.state.size){
                            this.interval()
                        }
                    })
                })
        }else {
            axios.get('http://www.niuduxiu.com/authentication/findzuzhi.jhtml?cpageNumber=1&cpageSize=1&zpageNumber='+this.state.size+'&zpageSize=6&type='+this.props.match.params.id)
                .then((item) => {
                    let total = Math.ceil(item.data.total / 6)
                    console.log(total)
                    this.setState({
                        School: this.state.School.concat(item.data.zuzhilist),
                        size: item.data.pageable.pageNumber,
                        total: total
                    }, () => {
                        if (this.state.total > this.state.size) {
                            this.interval()
                        }
                    })
                })
        }
    }
    toDetail(item){
        if(this.props.match.params.id==1){
           this.props.history.push('/SchoolDetail/'+item.na.id)
        }
    }
    render() {
        return (
            <div className="School">
                <HeaderImg items={this.state.items}  />
                <div className='SchoolItems'>
                    {
                        this.state.School.map((item,index)=>{
                            return(
                                <div className='SchoolOne' key={index} onClick={this.toDetail.bind(this,item)}>
                                    <img src={item.na.raddress} alt=""/>
                                    <p>{item.na.teamName}</p>
                                </div>
                            )
                        })
                    }
                </div>
                <Dibu size={this.state.size} total={this.state.total} />
            </div>
        );
    }
}

export default School;
